{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "from dagmm import dagmm\n",
    "import sys\n",
    "sys.path.append('../../common/')\n",
    "from evaluator import *\n",
    "import warnings\n",
    "import os\n",
    "import numpy as np\n",
    "warnings.filterwarnings(\"ignore\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def load_dataset(dataset, machine):\n",
    "    folder = os.path.join(\"../../processed\", dataset)\n",
    "    if not os.path.exists(folder):\n",
    "        raise Exception(\"Processed Data not found.\")\n",
    "    loader = []\n",
    "    for file in [\"train\", \"test\", \"labels\"]:\n",
    "        file = machine + \"_\" + file\n",
    "        loader.append(np.load(os.path.join(folder, f\"{file}.npy\")))\n",
    "    ## 准备数据\n",
    "    train_data = loader[0]\n",
    "    test_data = loader[1]\n",
    "    labels = np.zeros((loader[2].shape[0], 1))\n",
    "    for i, row in enumerate(loader[2]):\n",
    "        if np.any(row == 1):\n",
    "            labels[i] = 1   \n",
    "    return (train_data, test_data, labels)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## SMD"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "total datas: 28\n"
     ]
    }
   ],
   "source": [
    "origin_file_list = os.listdir(\"../../data/SMD/interpretation_label\")\n",
    "file_name_list = []\n",
    "for origin_file in origin_file_list:\n",
    "    file_name_list.append(origin_file[:-4])\n",
    "file_name_list.sort()\n",
    "\n",
    "datas = {}\n",
    "for file_name in file_name_list:\n",
    "    train_data, test_data, labels = load_dataset(\"SMD\", file_name)\n",
    "    datas[file_name] = (train_data, test_data, labels)\n",
    "    # print(f\"train_data shape: {train_data.shape}\")\n",
    "    # print(f\"test_data shape: {test_data.shape}\")\n",
    "    # print(f\"labels shape: {labels.shape}\")\n",
    "print(f\"total datas: {len(datas)}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " epoch 10/100 : loss = 27.560\n",
      " epoch 20/100 : loss = 23.170\n",
      " epoch 30/100 : loss = 19.802\n",
      " epoch 40/100 : loss = 17.749\n",
      " epoch 50/100 : loss = 16.396\n",
      " epoch 60/100 : loss = 15.364\n",
      " epoch 70/100 : loss = 14.507\n",
      " epoch 80/100 : loss = 13.756\n",
      " epoch 90/100 : loss = 13.077\n",
      " epoch 100/100 : loss = 12.463\n",
      " epoch 10/100 : loss = 33.049\n",
      " epoch 20/100 : loss = 31.270\n",
      " epoch 30/100 : loss = 29.834\n",
      " epoch 40/100 : loss = 28.782\n",
      " epoch 50/100 : loss = 28.005\n",
      " epoch 60/100 : loss = 27.373\n",
      " epoch 70/100 : loss = 26.778\n",
      " epoch 80/100 : loss = 26.162\n",
      " epoch 90/100 : loss = 25.516\n",
      " epoch 100/100 : loss = 24.883\n",
      " epoch 10/100 : loss = 32.167\n",
      " epoch 20/100 : loss = 29.410\n",
      " epoch 30/100 : loss = 26.907\n",
      " epoch 40/100 : loss = 24.851\n",
      " epoch 50/100 : loss = 23.393\n",
      " epoch 60/100 : loss = 22.463\n",
      " epoch 70/100 : loss = 21.736\n",
      " epoch 80/100 : loss = 21.060\n",
      " epoch 90/100 : loss = 20.429\n",
      " epoch 100/100 : loss = 19.877\n",
      " epoch 10/100 : loss = 32.213\n",
      " epoch 20/100 : loss = 29.374\n",
      " epoch 30/100 : loss = 26.834\n",
      " epoch 40/100 : loss = 24.925\n",
      " epoch 50/100 : loss = 23.588\n",
      " epoch 60/100 : loss = 22.599\n",
      " epoch 70/100 : loss = 21.804\n",
      " epoch 80/100 : loss = 21.109\n",
      " epoch 90/100 : loss = 20.488\n",
      " epoch 100/100 : loss = 19.929\n",
      " epoch 10/100 : loss = 28.155\n",
      " epoch 20/100 : loss = 24.753\n",
      " epoch 30/100 : loss = 21.950\n",
      " epoch 40/100 : loss = 19.982\n",
      " epoch 50/100 : loss = 18.637\n",
      " epoch 60/100 : loss = 17.610\n",
      " epoch 70/100 : loss = 16.754\n",
      " epoch 80/100 : loss = 16.026\n",
      " epoch 90/100 : loss = 15.399\n",
      " epoch 100/100 : loss = 14.873\n",
      " epoch 10/100 : loss = 31.891\n",
      " epoch 20/100 : loss = 28.367\n",
      " epoch 30/100 : loss = 25.285\n",
      " epoch 40/100 : loss = 23.120\n",
      " epoch 50/100 : loss = 21.684\n",
      " epoch 60/100 : loss = 20.715\n",
      " epoch 70/100 : loss = 19.993\n",
      " epoch 80/100 : loss = 19.417\n",
      " epoch 90/100 : loss = 18.934\n",
      " epoch 100/100 : loss = 18.508\n",
      " epoch 10/100 : loss = 31.786\n",
      " epoch 20/100 : loss = 28.678\n",
      " epoch 30/100 : loss = 25.950\n",
      " epoch 40/100 : loss = 23.993\n",
      " epoch 50/100 : loss = 22.809\n",
      " epoch 60/100 : loss = 22.107\n",
      " epoch 70/100 : loss = 21.626\n",
      " epoch 80/100 : loss = 21.234\n",
      " epoch 90/100 : loss = 20.855\n",
      " epoch 100/100 : loss = 20.497\n",
      " epoch 10/100 : loss = 29.089\n",
      " epoch 20/100 : loss = 25.655\n",
      " epoch 30/100 : loss = 22.369\n",
      " epoch 40/100 : loss = 20.042\n",
      " epoch 50/100 : loss = 18.480\n",
      " epoch 60/100 : loss = 17.374\n",
      " epoch 70/100 : loss = 16.530\n",
      " epoch 80/100 : loss = 15.846\n",
      " epoch 90/100 : loss = 15.275\n",
      " epoch 100/100 : loss = 14.777\n",
      " epoch 10/100 : loss = 31.348\n",
      " epoch 20/100 : loss = 28.433\n",
      " epoch 30/100 : loss = 26.135\n",
      " epoch 40/100 : loss = 24.498\n",
      " epoch 50/100 : loss = 23.282\n",
      " epoch 60/100 : loss = 22.254\n",
      " epoch 70/100 : loss = 21.359\n",
      " epoch 80/100 : loss = 20.570\n",
      " epoch 90/100 : loss = 19.883\n",
      " epoch 100/100 : loss = 19.289\n",
      " epoch 10/100 : loss = 29.479\n",
      " epoch 20/100 : loss = 26.297\n",
      " epoch 30/100 : loss = 23.705\n",
      " epoch 40/100 : loss = 21.842\n",
      " epoch 50/100 : loss = 20.529\n",
      " epoch 60/100 : loss = 19.520\n",
      " epoch 70/100 : loss = 18.686\n",
      " epoch 80/100 : loss = 17.985\n",
      " epoch 90/100 : loss = 17.387\n",
      " epoch 100/100 : loss = 16.868\n",
      " epoch 10/100 : loss = 30.276\n",
      " epoch 20/100 : loss = 28.223\n",
      " epoch 30/100 : loss = 26.090\n",
      " epoch 40/100 : loss = 24.474\n",
      " epoch 50/100 : loss = 23.486\n",
      " epoch 60/100 : loss = 22.884\n",
      " epoch 70/100 : loss = 22.468\n",
      " epoch 80/100 : loss = 22.118\n",
      " epoch 90/100 : loss = 21.800\n",
      " epoch 100/100 : loss = 21.488\n",
      " epoch 10/100 : loss = 29.223\n",
      " epoch 20/100 : loss = 26.179\n",
      " epoch 30/100 : loss = 22.921\n",
      " epoch 40/100 : loss = 20.396\n",
      " epoch 50/100 : loss = 18.770\n",
      " epoch 60/100 : loss = 17.626\n",
      " epoch 70/100 : loss = 16.729\n",
      " epoch 80/100 : loss = 15.985\n",
      " epoch 90/100 : loss = 15.365\n",
      " epoch 100/100 : loss = 14.845\n",
      " epoch 10/100 : loss = 29.698\n",
      " epoch 20/100 : loss = 27.282\n",
      " epoch 30/100 : loss = 24.814\n",
      " epoch 40/100 : loss = 22.890\n",
      " epoch 50/100 : loss = 21.562\n",
      " epoch 60/100 : loss = 20.616\n",
      " epoch 70/100 : loss = 19.878\n",
      " epoch 80/100 : loss = 19.267\n",
      " epoch 90/100 : loss = 18.745\n",
      " epoch 100/100 : loss = 18.291\n",
      " epoch 10/100 : loss = 26.850\n",
      " epoch 20/100 : loss = 21.929\n",
      " epoch 30/100 : loss = 17.437\n",
      " epoch 40/100 : loss = 14.511\n",
      " epoch 50/100 : loss = 12.698\n",
      " epoch 60/100 : loss = 11.513\n",
      " epoch 70/100 : loss = 10.681\n",
      " epoch 80/100 : loss = 10.048\n",
      " epoch 90/100 : loss = 9.540\n",
      " epoch 100/100 : loss = 9.123\n",
      " epoch 10/100 : loss = 28.755\n",
      " epoch 20/100 : loss = 26.242\n",
      " epoch 30/100 : loss = 23.687\n",
      " epoch 40/100 : loss = 21.693\n",
      " epoch 50/100 : loss = 20.301\n",
      " epoch 60/100 : loss = 19.306\n",
      " epoch 70/100 : loss = 18.538\n",
      " epoch 80/100 : loss = 17.902\n",
      " epoch 90/100 : loss = 17.350\n",
      " epoch 100/100 : loss = 16.874\n",
      " epoch 10/100 : loss = 27.738\n",
      " epoch 20/100 : loss = 23.619\n",
      " epoch 30/100 : loss = 20.175\n",
      " epoch 40/100 : loss = 17.929\n",
      " epoch 50/100 : loss = 16.474\n",
      " epoch 60/100 : loss = 15.453\n",
      " epoch 70/100 : loss = 14.682\n",
      " epoch 80/100 : loss = 14.066\n",
      " epoch 90/100 : loss = 13.551\n",
      " epoch 100/100 : loss = 13.113\n",
      " epoch 10/100 : loss = 28.953\n",
      " epoch 20/100 : loss = 26.003\n",
      " epoch 30/100 : loss = 23.824\n",
      " epoch 40/100 : loss = 22.326\n",
      " epoch 50/100 : loss = 21.170\n",
      " epoch 60/100 : loss = 20.159\n",
      " epoch 70/100 : loss = 19.238\n",
      " epoch 80/100 : loss = 18.411\n",
      " epoch 90/100 : loss = 17.686\n",
      " epoch 100/100 : loss = 17.022\n",
      " epoch 10/100 : loss = 27.204\n",
      " epoch 20/100 : loss = 22.878\n",
      " epoch 30/100 : loss = 20.033\n",
      " epoch 40/100 : loss = 18.172\n",
      " epoch 50/100 : loss = 16.818\n",
      " epoch 60/100 : loss = 15.729\n",
      " epoch 70/100 : loss = 14.790\n",
      " epoch 80/100 : loss = 13.978\n",
      " epoch 90/100 : loss = 13.296\n",
      " epoch 100/100 : loss = 12.726\n",
      " epoch 10/100 : loss = 28.243\n",
      " epoch 20/100 : loss = 25.212\n",
      " epoch 30/100 : loss = 22.263\n",
      " epoch 40/100 : loss = 19.910\n",
      " epoch 50/100 : loss = 18.186\n",
      " epoch 60/100 : loss = 16.966\n",
      " epoch 70/100 : loss = 16.062\n",
      " epoch 80/100 : loss = 15.322\n",
      " epoch 90/100 : loss = 14.663\n",
      " epoch 100/100 : loss = 14.061\n",
      " epoch 10/100 : loss = 27.020\n",
      " epoch 20/100 : loss = 22.967\n",
      " epoch 30/100 : loss = 20.194\n",
      " epoch 40/100 : loss = 18.442\n",
      " epoch 50/100 : loss = 17.396\n",
      " epoch 60/100 : loss = 16.698\n",
      " epoch 70/100 : loss = 16.149\n",
      " epoch 80/100 : loss = 15.646\n",
      " epoch 90/100 : loss = 15.145\n",
      " epoch 100/100 : loss = 14.675\n",
      " epoch 10/100 : loss = 31.468\n",
      " epoch 20/100 : loss = 29.405\n",
      " epoch 30/100 : loss = 27.572\n",
      " epoch 40/100 : loss = 26.072\n",
      " epoch 50/100 : loss = 24.824\n",
      " epoch 60/100 : loss = 23.768\n",
      " epoch 70/100 : loss = 22.865\n",
      " epoch 80/100 : loss = 22.069\n",
      " epoch 90/100 : loss = 21.376\n",
      " epoch 100/100 : loss = 20.764\n",
      " epoch 10/100 : loss = 35.100\n",
      " epoch 20/100 : loss = 31.729\n",
      " epoch 30/100 : loss = 28.471\n",
      " epoch 40/100 : loss = 25.688\n",
      " epoch 50/100 : loss = 23.547\n",
      " epoch 60/100 : loss = 22.024\n",
      " epoch 70/100 : loss = 20.951\n",
      " epoch 80/100 : loss = 20.159\n",
      " epoch 90/100 : loss = 19.546\n",
      " epoch 100/100 : loss = 19.042\n",
      " epoch 10/100 : loss = 28.410\n",
      " epoch 20/100 : loss = 24.988\n",
      " epoch 30/100 : loss = 22.051\n",
      " epoch 40/100 : loss = 19.972\n",
      " epoch 50/100 : loss = 18.588\n",
      " epoch 60/100 : loss = 17.614\n",
      " epoch 70/100 : loss = 16.847\n",
      " epoch 80/100 : loss = 16.188\n",
      " epoch 90/100 : loss = 15.599\n",
      " epoch 100/100 : loss = 15.067\n",
      " epoch 10/100 : loss = 27.172\n",
      " epoch 20/100 : loss = 24.333\n",
      " epoch 30/100 : loss = 22.085\n",
      " epoch 40/100 : loss = 20.538\n",
      " epoch 50/100 : loss = 19.470\n",
      " epoch 60/100 : loss = 18.642\n",
      " epoch 70/100 : loss = 17.922\n",
      " epoch 80/100 : loss = 17.252\n",
      " epoch 90/100 : loss = 16.619\n",
      " epoch 100/100 : loss = 16.040\n",
      " epoch 10/100 : loss = 25.955\n",
      " epoch 20/100 : loss = 20.619\n",
      " epoch 30/100 : loss = 16.271\n",
      " epoch 40/100 : loss = 13.516\n",
      " epoch 50/100 : loss = 11.803\n",
      " epoch 60/100 : loss = 10.683\n",
      " epoch 70/100 : loss = 9.891\n",
      " epoch 80/100 : loss = 9.315\n",
      " epoch 90/100 : loss = 8.887\n",
      " epoch 100/100 : loss = 8.552\n",
      " epoch 10/100 : loss = 30.980\n",
      " epoch 20/100 : loss = 26.671\n",
      " epoch 30/100 : loss = 23.772\n",
      " epoch 40/100 : loss = 21.927\n",
      " epoch 50/100 : loss = 20.531\n",
      " epoch 60/100 : loss = 19.367\n",
      " epoch 70/100 : loss = 18.407\n",
      " epoch 80/100 : loss = 17.639\n",
      " epoch 90/100 : loss = 17.018\n",
      " epoch 100/100 : loss = 16.516\n",
      " epoch 10/100 : loss = 28.556\n",
      " epoch 20/100 : loss = 25.569\n",
      " epoch 30/100 : loss = 23.461\n",
      " epoch 40/100 : loss = 21.949\n",
      " epoch 50/100 : loss = 20.721\n",
      " epoch 60/100 : loss = 19.669\n",
      " epoch 70/100 : loss = 18.763\n",
      " epoch 80/100 : loss = 17.984\n",
      " epoch 90/100 : loss = 17.315\n",
      " epoch 100/100 : loss = 16.734\n",
      " epoch 10/100 : loss = 27.310\n",
      " epoch 20/100 : loss = 22.749\n",
      " epoch 30/100 : loss = 19.587\n",
      " epoch 40/100 : loss = 17.371\n",
      " epoch 50/100 : loss = 15.729\n",
      " epoch 60/100 : loss = 14.406\n",
      " epoch 70/100 : loss = 13.329\n",
      " epoch 80/100 : loss = 12.472\n",
      " epoch 90/100 : loss = 11.794\n",
      " epoch 100/100 : loss = 11.237\n"
     ]
    }
   ],
   "source": [
    "results = {}\n",
    "for machine in file_name_list:\n",
    "        # print(machine)\n",
    "        train_data,test_data, labels = datas[machine]\n",
    "        ratio = np.count_nonzero(labels == 1)/labels.shape[0]\n",
    "        dagmm_model = dagmm.DAGMM(epoch_size=100, contamination=ratio, comp_hiddens=[8,4])\n",
    "        dagmm_model.fit(train_data)\n",
    "        anomaly_score = dagmm_model.decision_function(test_data).T\n",
    "        result = bf_search(labels, anomaly_score, verbose = False, is_adjust=True)\n",
    "        results[machine] = result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "precision mean: 0.8464\n",
      "recall mean: 0.8737\n",
      "f1 mean: 0.8493\n",
      "f1* mean: 0.8598\n"
     ]
    }
   ],
   "source": [
    "f1_scores = [item['f1-score'] for item in list(results.values())]\n",
    "precisions = [item['precision'] for item in list(results.values())]\n",
    "recalls = [item['recall'] for item in list(results.values())]\n",
    "TNs = [item['TN'] for item in list(results.values())]\n",
    "TPs = [item['TP'] for item in list(results.values())]\n",
    "FNs = [item['FN'] for item in list(results.values())]\n",
    "FPs = [item['FP'] for item in list(results.values())]\n",
    "f1_mean = round(np.mean(f1_scores).item(),4)\n",
    "precision_mean = round(np.mean(precisions),4)\n",
    "recall_mean = round(np.mean(recalls),4)\n",
    "f1_star = round(2 * precision_mean * recall_mean / (precision_mean + recall_mean + 0.00001),4)\n",
    "print(f\"precision mean: {precision_mean}\")\n",
    "print(f\"recall mean: {recall_mean}\")\n",
    "print(f\"f1 mean: {f1_mean}\")\n",
    "print(f\"f1* mean: {f1_star}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## SMAP\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "origin_file_list = os.listdir(\"../../processed/SMAP\")\n",
    "file_name_set = set()\n",
    "for origin_file in origin_file_list:\n",
    "    file_name_set.add(origin_file.split(\"_\")[0])\n",
    "file_name_list = list(file_name_set)\n",
    "file_name_list.sort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "datas = {}\n",
    "for file_name in file_name_list:\n",
    "    train_data, test_data, labels = load_dataset(\"SMAP\", file_name)\n",
    "    datas[file_name] = (train_data, test_data, labels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " epoch 10/100 : loss = 19.481\n",
      " epoch 20/100 : loss = 18.951\n",
      " epoch 30/100 : loss = 18.755\n",
      " epoch 40/100 : loss = 18.640\n",
      " epoch 50/100 : loss = 18.559\n",
      " epoch 60/100 : loss = 18.498\n",
      " epoch 70/100 : loss = 18.448\n",
      " epoch 80/100 : loss = 18.406\n",
      " epoch 90/100 : loss = 18.369\n",
      " epoch 100/100 : loss = 18.336\n",
      " epoch 10/100 : loss = 18.136\n",
      " epoch 20/100 : loss = 18.093\n",
      " epoch 30/100 : loss = 18.062\n",
      " epoch 40/100 : loss = 18.034\n",
      " epoch 50/100 : loss = 18.005\n",
      " epoch 60/100 : loss = 17.971\n",
      " epoch 70/100 : loss = 17.942\n",
      " epoch 80/100 : loss = 17.914\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " epoch 90/100 : loss = 17.885\n",
      " epoch 100/100 : loss = 17.854\n",
      " epoch 10/100 : loss = 18.039\n",
      " epoch 20/100 : loss = 18.001\n",
      " epoch 30/100 : loss = 17.970\n",
      " epoch 40/100 : loss = 17.940\n",
      " epoch 50/100 : loss = 17.914\n",
      " epoch 60/100 : loss = 17.888\n",
      " epoch 70/100 : loss = 17.863\n",
      " epoch 80/100 : loss = 17.838\n",
      " epoch 90/100 : loss = 17.809\n",
      " epoch 100/100 : loss = 17.782\n",
      " epoch 10/100 : loss = 18.046\n",
      " epoch 20/100 : loss = 18.010\n",
      " epoch 30/100 : loss = 17.982\n",
      " epoch 40/100 : loss = 17.957\n",
      " epoch 50/100 : loss = 17.929\n",
      " epoch 60/100 : loss = 17.902\n",
      " epoch 70/100 : loss = 17.879\n",
      " epoch 80/100 : loss = 17.856\n",
      " epoch 90/100 : loss = 17.833\n",
      " epoch 100/100 : loss = 17.809\n",
      " epoch 10/100 : loss = 10.985\n",
      " epoch 20/100 : loss = 10.968\n",
      " epoch 30/100 : loss = 10.953\n",
      " epoch 40/100 : loss = 10.938\n",
      " epoch 50/100 : loss = 10.925\n",
      " epoch 60/100 : loss = 10.913\n",
      " epoch 70/100 : loss = 10.903\n",
      " epoch 80/100 : loss = 10.894\n",
      " epoch 90/100 : loss = 10.886\n",
      " epoch 100/100 : loss = 10.877\n",
      " epoch 10/100 : loss = 11.025\n",
      " epoch 20/100 : loss = 10.995\n",
      " epoch 30/100 : loss = 10.967\n",
      " epoch 40/100 : loss = 10.941\n",
      " epoch 50/100 : loss = 10.917\n",
      " epoch 60/100 : loss = 10.896\n",
      " epoch 70/100 : loss = 10.877\n",
      " epoch 80/100 : loss = 10.861\n",
      " epoch 90/100 : loss = 10.845\n",
      " epoch 100/100 : loss = 10.832\n",
      " epoch 10/100 : loss = 10.137\n",
      " epoch 20/100 : loss = 10.009\n",
      " epoch 30/100 : loss = 9.962\n",
      " epoch 40/100 : loss = 9.937\n",
      " epoch 50/100 : loss = 9.920\n",
      " epoch 60/100 : loss = 9.909\n",
      " epoch 70/100 : loss = 9.900\n",
      " epoch 80/100 : loss = 9.892\n",
      " epoch 90/100 : loss = 9.885\n",
      " epoch 100/100 : loss = 9.879\n",
      " epoch 10/100 : loss = 10.576\n",
      " epoch 20/100 : loss = 10.263\n",
      " epoch 30/100 : loss = 9.881\n",
      " epoch 40/100 : loss = 9.842\n",
      " epoch 50/100 : loss = 9.829\n",
      " epoch 60/100 : loss = 9.810\n",
      " epoch 70/100 : loss = 9.803\n",
      " epoch 80/100 : loss = 9.795\n",
      " epoch 90/100 : loss = 9.788\n",
      " epoch 100/100 : loss = 9.782\n",
      " epoch 10/100 : loss = 10.432\n",
      " epoch 20/100 : loss = 10.148\n",
      " epoch 30/100 : loss = 10.071\n",
      " epoch 40/100 : loss = 10.005\n",
      " epoch 50/100 : loss = 9.953\n",
      " epoch 60/100 : loss = 9.912\n",
      " epoch 70/100 : loss = 9.881\n",
      " epoch 80/100 : loss = 9.857\n",
      " epoch 90/100 : loss = 9.838\n",
      " epoch 100/100 : loss = 9.822\n",
      " epoch 10/100 : loss = 16.103\n",
      " epoch 20/100 : loss = 14.863\n",
      " epoch 30/100 : loss = 14.071\n",
      " epoch 40/100 : loss = 13.581\n",
      " epoch 50/100 : loss = 13.247\n",
      " epoch 60/100 : loss = 12.981\n",
      " epoch 70/100 : loss = 12.427\n",
      " epoch 80/100 : loss = 12.219\n",
      " epoch 90/100 : loss = 12.135\n",
      " epoch 100/100 : loss = 12.082\n",
      " epoch 10/100 : loss = 18.291\n",
      " epoch 20/100 : loss = 18.231\n",
      " epoch 30/100 : loss = 18.184\n",
      " epoch 40/100 : loss = 18.141\n",
      " epoch 50/100 : loss = 18.097\n",
      " epoch 60/100 : loss = 18.053\n",
      " epoch 70/100 : loss = 18.007\n",
      " epoch 80/100 : loss = 17.957\n",
      " epoch 90/100 : loss = 17.901\n",
      " epoch 100/100 : loss = 17.853\n",
      " epoch 10/100 : loss = 5.865\n",
      " epoch 20/100 : loss = 5.815\n",
      " epoch 30/100 : loss = 5.798\n",
      " epoch 40/100 : loss = 5.787\n",
      " epoch 50/100 : loss = 5.780\n",
      " epoch 60/100 : loss = 5.774\n",
      " epoch 70/100 : loss = 5.768\n",
      " epoch 80/100 : loss = 5.762\n",
      " epoch 90/100 : loss = 5.757\n",
      " epoch 100/100 : loss = 5.750\n",
      " epoch 10/100 : loss = 5.865\n",
      " epoch 20/100 : loss = 5.815\n",
      " epoch 30/100 : loss = 5.798\n",
      " epoch 40/100 : loss = 5.787\n",
      " epoch 50/100 : loss = 5.780\n",
      " epoch 60/100 : loss = 5.774\n",
      " epoch 70/100 : loss = 5.768\n",
      " epoch 80/100 : loss = 5.762\n",
      " epoch 90/100 : loss = 5.757\n",
      " epoch 100/100 : loss = 5.750\n",
      " epoch 10/100 : loss = 5.865\n",
      " epoch 20/100 : loss = 5.815\n",
      " epoch 30/100 : loss = 5.798\n",
      " epoch 40/100 : loss = 5.787\n",
      " epoch 50/100 : loss = 5.780\n",
      " epoch 60/100 : loss = 5.774\n",
      " epoch 70/100 : loss = 5.768\n",
      " epoch 80/100 : loss = 5.762\n",
      " epoch 90/100 : loss = 5.757\n",
      " epoch 100/100 : loss = 5.750\n",
      " epoch 10/100 : loss = 19.771\n",
      " epoch 20/100 : loss = 19.122\n",
      " epoch 30/100 : loss = 18.884\n",
      " epoch 40/100 : loss = 18.748\n",
      " epoch 50/100 : loss = 18.655\n",
      " epoch 60/100 : loss = 18.586\n",
      " epoch 70/100 : loss = 18.530\n",
      " epoch 80/100 : loss = 18.484\n",
      " epoch 90/100 : loss = 18.444\n",
      " epoch 100/100 : loss = 18.409\n",
      " epoch 10/100 : loss = 19.085\n",
      " epoch 20/100 : loss = 19.042\n",
      " epoch 30/100 : loss = 19.011\n",
      " epoch 40/100 : loss = 18.982\n",
      " epoch 50/100 : loss = 18.953\n",
      " epoch 60/100 : loss = 18.924\n",
      " epoch 70/100 : loss = 18.894\n",
      " epoch 80/100 : loss = 18.864\n",
      " epoch 90/100 : loss = 18.835\n",
      " epoch 100/100 : loss = 18.807\n",
      " epoch 10/100 : loss = 19.091\n",
      " epoch 20/100 : loss = 19.045\n",
      " epoch 30/100 : loss = 19.008\n",
      " epoch 40/100 : loss = 18.974\n",
      " epoch 50/100 : loss = 18.940\n",
      " epoch 60/100 : loss = 18.908\n",
      " epoch 70/100 : loss = 18.876\n",
      " epoch 80/100 : loss = 18.844\n",
      " epoch 90/100 : loss = 18.815\n",
      " epoch 100/100 : loss = 18.786\n",
      " epoch 10/100 : loss = 16.083\n",
      " epoch 20/100 : loss = 16.042\n",
      " epoch 30/100 : loss = 16.012\n",
      " epoch 40/100 : loss = 15.986\n",
      " epoch 50/100 : loss = 15.961\n",
      " epoch 60/100 : loss = 15.936\n",
      " epoch 70/100 : loss = 15.908\n",
      " epoch 80/100 : loss = 15.873\n",
      " epoch 90/100 : loss = 15.831\n",
      " epoch 100/100 : loss = 15.791\n",
      " epoch 10/100 : loss = 16.063\n",
      " epoch 20/100 : loss = 16.021\n",
      " epoch 30/100 : loss = 15.991\n",
      " epoch 40/100 : loss = 15.966\n",
      " epoch 50/100 : loss = 15.942\n",
      " epoch 60/100 : loss = 15.915\n",
      " epoch 70/100 : loss = 15.887\n",
      " epoch 80/100 : loss = 15.861\n",
      " epoch 90/100 : loss = 15.835\n",
      " epoch 100/100 : loss = 15.807\n",
      " epoch 10/100 : loss = 20.392\n",
      " epoch 20/100 : loss = 19.312\n",
      " epoch 30/100 : loss = 18.989\n",
      " epoch 40/100 : loss = 18.807\n",
      " epoch 50/100 : loss = 18.681\n",
      " epoch 60/100 : loss = 18.586\n",
      " epoch 70/100 : loss = 18.509\n",
      " epoch 80/100 : loss = 18.446\n",
      " epoch 90/100 : loss = 18.392\n",
      " epoch 100/100 : loss = 18.346\n",
      " epoch 10/100 : loss = 26.482\n",
      " epoch 20/100 : loss = 23.162\n",
      " epoch 30/100 : loss = 21.716\n",
      " epoch 40/100 : loss = 20.726\n",
      " epoch 50/100 : loss = 19.981\n",
      " epoch 60/100 : loss = 19.391\n",
      " epoch 70/100 : loss = 18.906\n",
      " epoch 80/100 : loss = 18.494\n",
      " epoch 90/100 : loss = 18.134\n",
      " epoch 100/100 : loss = 17.813\n",
      " epoch 10/100 : loss = 18.925\n",
      " epoch 20/100 : loss = 17.086\n",
      " epoch 30/100 : loss = 16.275\n",
      " epoch 40/100 : loss = 15.737\n",
      " epoch 50/100 : loss = 15.340\n",
      " epoch 60/100 : loss = 15.032\n",
      " epoch 70/100 : loss = 14.786\n",
      " epoch 80/100 : loss = 14.585\n",
      " epoch 90/100 : loss = 14.416\n",
      " epoch 100/100 : loss = 14.273\n",
      " epoch 10/100 : loss = 18.721\n",
      " epoch 20/100 : loss = 18.553\n",
      " epoch 30/100 : loss = 18.454\n",
      " epoch 40/100 : loss = 18.377\n",
      " epoch 50/100 : loss = 18.310\n",
      " epoch 60/100 : loss = 18.242\n",
      " epoch 70/100 : loss = 18.194\n",
      " epoch 80/100 : loss = 18.155\n",
      " epoch 90/100 : loss = 18.115\n",
      " epoch 100/100 : loss = 18.078\n",
      " epoch 10/100 : loss = 18.696\n",
      " epoch 20/100 : loss = 18.531\n",
      " epoch 30/100 : loss = 18.434\n",
      " epoch 40/100 : loss = 18.361\n",
      " epoch 50/100 : loss = 18.297\n",
      " epoch 60/100 : loss = 18.234\n",
      " epoch 70/100 : loss = 18.179\n",
      " epoch 80/100 : loss = 18.139\n",
      " epoch 90/100 : loss = 18.103\n",
      " epoch 100/100 : loss = 18.068\n",
      " epoch 10/100 : loss = 18.751\n",
      " epoch 20/100 : loss = 18.574\n",
      " epoch 30/100 : loss = 18.469\n",
      " epoch 40/100 : loss = 18.389\n",
      " epoch 50/100 : loss = 18.317\n",
      " epoch 60/100 : loss = 18.249\n",
      " epoch 70/100 : loss = 18.207\n",
      " epoch 80/100 : loss = 18.169\n",
      " epoch 90/100 : loss = 18.132\n",
      " epoch 100/100 : loss = 18.097\n",
      " epoch 10/100 : loss = 18.735\n",
      " epoch 20/100 : loss = 18.565\n",
      " epoch 30/100 : loss = 18.464\n",
      " epoch 40/100 : loss = 18.387\n",
      " epoch 50/100 : loss = 18.319\n",
      " epoch 60/100 : loss = 18.246\n",
      " epoch 70/100 : loss = 18.205\n",
      " epoch 80/100 : loss = 18.167\n",
      " epoch 90/100 : loss = 18.129\n",
      " epoch 100/100 : loss = 18.093\n",
      " epoch 10/100 : loss = 19.531\n",
      " epoch 20/100 : loss = 19.423\n",
      " epoch 30/100 : loss = 19.346\n",
      " epoch 40/100 : loss = 19.277\n",
      " epoch 50/100 : loss = 19.205\n",
      " epoch 60/100 : loss = 19.158\n",
      " epoch 70/100 : loss = 19.114\n",
      " epoch 80/100 : loss = 19.073\n",
      " epoch 90/100 : loss = 19.033\n",
      " epoch 100/100 : loss = 18.995\n",
      " epoch 10/100 : loss = 18.376\n",
      " epoch 20/100 : loss = 18.306\n",
      " epoch 30/100 : loss = 18.256\n",
      " epoch 40/100 : loss = 18.216\n",
      " epoch 50/100 : loss = 18.180\n",
      " epoch 60/100 : loss = 18.146\n",
      " epoch 70/100 : loss = 18.111\n",
      " epoch 80/100 : loss = 18.074\n",
      " epoch 90/100 : loss = 18.035\n",
      " epoch 100/100 : loss = 17.992\n",
      " epoch 10/100 : loss = 18.093\n",
      " epoch 20/100 : loss = 18.048\n",
      " epoch 30/100 : loss = 18.016\n",
      " epoch 40/100 : loss = 17.988\n",
      " epoch 50/100 : loss = 17.960\n",
      " epoch 60/100 : loss = 17.932\n",
      " epoch 70/100 : loss = 17.905\n",
      " epoch 80/100 : loss = 17.874\n",
      " epoch 90/100 : loss = 17.834\n",
      " epoch 100/100 : loss = 17.795\n",
      " epoch 10/100 : loss = 18.272\n",
      " epoch 20/100 : loss = 18.211\n",
      " epoch 30/100 : loss = 18.169\n",
      " epoch 40/100 : loss = 18.134\n",
      " epoch 50/100 : loss = 18.101\n",
      " epoch 60/100 : loss = 18.067\n",
      " epoch 70/100 : loss = 18.025\n",
      " epoch 80/100 : loss = 17.973\n",
      " epoch 90/100 : loss = 17.932\n",
      " epoch 100/100 : loss = 17.886\n",
      " epoch 10/100 : loss = 18.655\n",
      " epoch 20/100 : loss = 18.496\n",
      " epoch 30/100 : loss = 18.404\n",
      " epoch 40/100 : loss = 18.335\n",
      " epoch 50/100 : loss = 18.275\n",
      " epoch 60/100 : loss = 18.214\n",
      " epoch 70/100 : loss = 18.163\n",
      " epoch 80/100 : loss = 18.124\n",
      " epoch 90/100 : loss = 18.090\n",
      " epoch 100/100 : loss = 18.057\n",
      " epoch 10/100 : loss = 19.178\n",
      " epoch 20/100 : loss = 18.883\n",
      " epoch 30/100 : loss = 18.716\n",
      " epoch 40/100 : loss = 18.595\n",
      " epoch 50/100 : loss = 18.500\n",
      " epoch 60/100 : loss = 18.420\n",
      " epoch 70/100 : loss = 18.351\n",
      " epoch 80/100 : loss = 18.286\n",
      " epoch 90/100 : loss = 18.230\n",
      " epoch 100/100 : loss = 18.200\n",
      " epoch 10/100 : loss = 18.274\n",
      " epoch 20/100 : loss = 18.210\n",
      " epoch 30/100 : loss = 18.151\n",
      " epoch 40/100 : loss = 18.089\n",
      " epoch 50/100 : loss = 18.044\n",
      " epoch 60/100 : loss = 18.003\n",
      " epoch 70/100 : loss = 17.962\n",
      " epoch 80/100 : loss = 17.923\n",
      " epoch 90/100 : loss = 17.892\n",
      " epoch 100/100 : loss = 17.863\n",
      " epoch 10/100 : loss = 18.215\n",
      " epoch 20/100 : loss = 18.168\n",
      " epoch 30/100 : loss = 18.134\n",
      " epoch 40/100 : loss = 18.104\n",
      " epoch 50/100 : loss = 18.076\n",
      " epoch 60/100 : loss = 18.046\n",
      " epoch 70/100 : loss = 18.012\n",
      " epoch 80/100 : loss = 17.981\n",
      " epoch 90/100 : loss = 17.945\n",
      " epoch 100/100 : loss = 17.896\n",
      " epoch 10/100 : loss = 18.119\n",
      " epoch 20/100 : loss = 18.072\n",
      " epoch 30/100 : loss = 18.039\n",
      " epoch 40/100 : loss = 18.010\n",
      " epoch 50/100 : loss = 17.981\n",
      " epoch 60/100 : loss = 17.948\n",
      " epoch 70/100 : loss = 17.904\n",
      " epoch 80/100 : loss = 17.856\n",
      " epoch 90/100 : loss = 17.819\n",
      " epoch 100/100 : loss = 17.788\n",
      " epoch 10/100 : loss = 17.194\n",
      " epoch 20/100 : loss = 17.118\n",
      " epoch 30/100 : loss = 17.061\n",
      " epoch 40/100 : loss = 17.011\n",
      " epoch 50/100 : loss = 16.960\n",
      " epoch 60/100 : loss = 16.918\n",
      " epoch 70/100 : loss = 16.882\n",
      " epoch 80/100 : loss = 16.844\n",
      " epoch 90/100 : loss = 16.802\n",
      " epoch 100/100 : loss = 16.770\n",
      " epoch 10/100 : loss = 13.954\n",
      " epoch 20/100 : loss = 13.795\n",
      " epoch 30/100 : loss = 13.764\n",
      " epoch 40/100 : loss = 13.744\n",
      " epoch 50/100 : loss = 13.727\n",
      " epoch 60/100 : loss = 13.713\n",
      " epoch 70/100 : loss = 13.701\n",
      " epoch 80/100 : loss = 13.689\n",
      " epoch 90/100 : loss = 13.679\n",
      " epoch 100/100 : loss = 13.669\n",
      " epoch 10/100 : loss = 18.099\n",
      " epoch 20/100 : loss = 18.058\n",
      " epoch 30/100 : loss = 18.026\n",
      " epoch 40/100 : loss = 17.996\n",
      " epoch 50/100 : loss = 17.965\n",
      " epoch 60/100 : loss = 17.938\n",
      " epoch 70/100 : loss = 17.911\n",
      " epoch 80/100 : loss = 17.882\n",
      " epoch 90/100 : loss = 17.847\n",
      " epoch 100/100 : loss = 17.805\n",
      " epoch 10/100 : loss = 18.014\n",
      " epoch 20/100 : loss = 17.977\n",
      " epoch 30/100 : loss = 17.951\n",
      " epoch 40/100 : loss = 17.927\n",
      " epoch 50/100 : loss = 17.905\n",
      " epoch 60/100 : loss = 17.882\n",
      " epoch 70/100 : loss = 17.859\n",
      " epoch 80/100 : loss = 17.836\n",
      " epoch 90/100 : loss = 17.816\n",
      " epoch 100/100 : loss = 17.796\n",
      " epoch 10/100 : loss = 19.436\n",
      " epoch 20/100 : loss = 18.534\n",
      " epoch 30/100 : loss = 18.172\n",
      " epoch 40/100 : loss = 17.995\n",
      " epoch 50/100 : loss = 17.871\n",
      " epoch 60/100 : loss = 17.773\n",
      " epoch 70/100 : loss = 17.690\n",
      " epoch 80/100 : loss = 17.619\n",
      " epoch 90/100 : loss = 17.556\n",
      " epoch 100/100 : loss = 17.501\n",
      " epoch 10/100 : loss = 19.579\n",
      " epoch 20/100 : loss = 18.846\n",
      " epoch 30/100 : loss = 18.566\n",
      " epoch 40/100 : loss = 18.443\n",
      " epoch 50/100 : loss = 18.358\n",
      " epoch 60/100 : loss = 18.295\n",
      " epoch 70/100 : loss = 18.245\n",
      " epoch 80/100 : loss = 18.204\n",
      " epoch 90/100 : loss = 18.165\n",
      " epoch 100/100 : loss = 18.130\n",
      " epoch 10/100 : loss = 12.977\n",
      " epoch 20/100 : loss = 12.835\n",
      " epoch 30/100 : loss = 12.769\n",
      " epoch 40/100 : loss = 12.724\n",
      " epoch 50/100 : loss = 12.687\n",
      " epoch 60/100 : loss = 12.652\n",
      " epoch 70/100 : loss = 12.619\n",
      " epoch 80/100 : loss = 12.589\n",
      " epoch 90/100 : loss = 12.562\n",
      " epoch 100/100 : loss = 12.542\n",
      " epoch 10/100 : loss = 12.788\n",
      " epoch 20/100 : loss = 12.769\n",
      " epoch 30/100 : loss = 12.753\n",
      " epoch 40/100 : loss = 12.736\n",
      " epoch 50/100 : loss = 12.720\n",
      " epoch 60/100 : loss = 12.701\n",
      " epoch 70/100 : loss = 12.684\n",
      " epoch 80/100 : loss = 12.668\n",
      " epoch 90/100 : loss = 12.654\n",
      " epoch 100/100 : loss = 12.639\n",
      " epoch 10/100 : loss = 5.353\n",
      " epoch 20/100 : loss = 4.230\n",
      " epoch 30/100 : loss = 3.792\n",
      " epoch 40/100 : loss = 3.507\n",
      " epoch 50/100 : loss = 3.295\n",
      " epoch 60/100 : loss = 3.128\n",
      " epoch 70/100 : loss = 2.989\n",
      " epoch 80/100 : loss = 2.870\n",
      " epoch 90/100 : loss = 2.764\n",
      " epoch 100/100 : loss = 2.669\n",
      " epoch 10/100 : loss = 16.216\n",
      " epoch 20/100 : loss = 16.169\n",
      " epoch 30/100 : loss = 16.133\n",
      " epoch 40/100 : loss = 16.103\n",
      " epoch 50/100 : loss = 16.075\n",
      " epoch 60/100 : loss = 16.042\n",
      " epoch 70/100 : loss = 16.011\n",
      " epoch 80/100 : loss = 15.970\n",
      " epoch 90/100 : loss = 15.935\n",
      " epoch 100/100 : loss = 15.900\n",
      " epoch 10/100 : loss = 18.048\n",
      " epoch 20/100 : loss = 18.006\n",
      " epoch 30/100 : loss = 17.970\n",
      " epoch 40/100 : loss = 17.936\n",
      " epoch 50/100 : loss = 17.904\n",
      " epoch 60/100 : loss = 17.876\n",
      " epoch 70/100 : loss = 17.850\n",
      " epoch 80/100 : loss = 17.825\n",
      " epoch 90/100 : loss = 17.798\n",
      " epoch 100/100 : loss = 17.771\n",
      " epoch 10/100 : loss = 18.053\n",
      " epoch 20/100 : loss = 18.003\n",
      " epoch 30/100 : loss = 17.962\n",
      " epoch 40/100 : loss = 17.925\n",
      " epoch 50/100 : loss = 17.894\n",
      " epoch 60/100 : loss = 17.865\n",
      " epoch 70/100 : loss = 17.836\n",
      " epoch 80/100 : loss = 17.803\n",
      " epoch 90/100 : loss = 17.765\n",
      " epoch 100/100 : loss = 17.726\n",
      " epoch 10/100 : loss = 19.595\n",
      " epoch 20/100 : loss = 18.261\n",
      " epoch 30/100 : loss = 17.949\n",
      " epoch 40/100 : loss = 17.795\n",
      " epoch 50/100 : loss = 17.697\n",
      " epoch 60/100 : loss = 17.628\n",
      " epoch 70/100 : loss = 17.576\n",
      " epoch 80/100 : loss = 17.535\n",
      " epoch 90/100 : loss = 17.501\n",
      " epoch 100/100 : loss = 17.472\n",
      " epoch 10/100 : loss = 7.800\n",
      " epoch 20/100 : loss = 7.787\n",
      " epoch 30/100 : loss = 7.773\n",
      " epoch 40/100 : loss = 7.762\n",
      " epoch 50/100 : loss = 7.750\n",
      " epoch 60/100 : loss = 7.736\n",
      " epoch 70/100 : loss = 7.720\n",
      " epoch 80/100 : loss = 7.704\n",
      " epoch 90/100 : loss = 7.689\n",
      " epoch 100/100 : loss = 7.676\n",
      " epoch 10/100 : loss = 16.371\n",
      " epoch 20/100 : loss = 13.627\n",
      " epoch 30/100 : loss = 12.189\n",
      " epoch 40/100 : loss = 11.225\n",
      " epoch 50/100 : loss = 10.526\n",
      " epoch 60/100 : loss = 10.000\n",
      " epoch 70/100 : loss = 9.594\n",
      " epoch 80/100 : loss = 9.272\n",
      " epoch 90/100 : loss = 9.012\n",
      " epoch 100/100 : loss = 8.796\n",
      " epoch 10/100 : loss = 18.148\n",
      " epoch 20/100 : loss = 18.082\n",
      " epoch 30/100 : loss = 18.031\n",
      " epoch 40/100 : loss = 17.984\n",
      " epoch 50/100 : loss = 17.936\n",
      " epoch 60/100 : loss = 17.898\n",
      " epoch 70/100 : loss = 17.861\n",
      " epoch 80/100 : loss = 17.821\n",
      " epoch 90/100 : loss = 17.777\n",
      " epoch 100/100 : loss = 17.750\n",
      " epoch 10/100 : loss = 3.575\n",
      " epoch 20/100 : loss = 3.266\n",
      " epoch 30/100 : loss = 3.166\n",
      " epoch 40/100 : loss = 3.108\n",
      " epoch 50/100 : loss = 3.069\n",
      " epoch 60/100 : loss = 3.041\n",
      " epoch 70/100 : loss = 3.020\n",
      " epoch 80/100 : loss = 3.004\n",
      " epoch 90/100 : loss = 2.991\n",
      " epoch 100/100 : loss = 2.980\n",
      " epoch 10/100 : loss = 14.072\n",
      " epoch 20/100 : loss = 13.957\n",
      " epoch 30/100 : loss = 13.911\n",
      " epoch 40/100 : loss = 13.883\n",
      " epoch 50/100 : loss = 13.862\n",
      " epoch 60/100 : loss = 13.846\n",
      " epoch 70/100 : loss = 13.831\n",
      " epoch 80/100 : loss = 13.818\n",
      " epoch 90/100 : loss = 13.806\n",
      " epoch 100/100 : loss = 13.793\n",
      " epoch 10/100 : loss = 11.864\n",
      " epoch 20/100 : loss = 11.843\n",
      " epoch 30/100 : loss = 11.832\n",
      " epoch 40/100 : loss = 11.823\n",
      " epoch 50/100 : loss = 11.814\n",
      " epoch 60/100 : loss = 11.805\n",
      " epoch 70/100 : loss = 11.797\n",
      " epoch 80/100 : loss = 11.788\n",
      " epoch 90/100 : loss = 11.779\n",
      " epoch 100/100 : loss = 11.769\n"
     ]
    }
   ],
   "source": [
    "results = {}\n",
    "for machine in file_name_list:\n",
    "        # print(machine)\n",
    "        train_data,test_data, labels = datas[machine]\n",
    "        if machine == \"D-12\" or machine == \"D-13\":\n",
    "                train_data,test_data, labels = datas[\"D-11\"]\n",
    "        ratio = np.count_nonzero(labels == 100)/labels.shape[0]+0.000001\n",
    "        dagmm_model = dagmm.DAGMM(epoch_size=100, contamination=ratio, comp_hiddens=[2,1])\n",
    "        dagmm_model.fit(train_data)\n",
    "        anomaly_score = dagmm_model.decision_function(test_data).T\n",
    "        result = bf_search(labels, anomaly_score, verbose = False, is_adjust=True)\n",
    "        results[machine] = result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "precision mean: 0.6946\n",
      "recall mean: 0.8251\n",
      "f1 mean: 0.7393\n",
      "f1* mean: 0.7542\n"
     ]
    }
   ],
   "source": [
    "f1_scores = [item['f1-score'] for item in list(results.values())]\n",
    "precisions = [item['precision'] for item in list(results.values())]\n",
    "recalls = [item['recall'] for item in list(results.values())]\n",
    "TNs = [item['TN'] for item in list(results.values())]\n",
    "TPs = [item['TP'] for item in list(results.values())]\n",
    "FNs = [item['FN'] for item in list(results.values())]\n",
    "FPs = [item['FP'] for item in list(results.values())]\n",
    "f1_mean = round(np.mean(f1_scores).item(),4)\n",
    "precision_mean = round(np.mean(precisions),4)\n",
    "recall_mean = round(np.mean(recalls),4)\n",
    "f1_star = round(2 * precision_mean * recall_mean / (precision_mean + recall_mean + 0.00001),4)\n",
    "print(f\"precision mean: {precision_mean}\")\n",
    "print(f\"recall mean: {recall_mean}\")\n",
    "print(f\"f1 mean: {f1_mean}\")\n",
    "print(f\"f1* mean: {f1_star}\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## MSL\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "origin_file_list = os.listdir(\"../../processed/MSL\")\n",
    "file_name_set = set()\n",
    "for origin_file in origin_file_list:\n",
    "    file_name_set.add(origin_file.split(\"_\")[0])\n",
    "file_name_list = list(file_name_set)\n",
    "file_name_list.sort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "datas = {}\n",
    "for file_name in file_name_list:\n",
    "    train_data, test_data, labels = load_dataset(\"MSL\", file_name)\n",
    "    datas[file_name] = (train_data, test_data, labels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " epoch 10/100 : loss = 16.730\n",
      " epoch 20/100 : loss = 16.006\n",
      " epoch 30/100 : loss = 15.723\n",
      " epoch 40/100 : loss = 15.560\n",
      " epoch 50/100 : loss = 15.448\n",
      " epoch 60/100 : loss = 15.367\n",
      " epoch 70/100 : loss = 15.306\n",
      " epoch 80/100 : loss = 15.257\n",
      " epoch 90/100 : loss = 15.219\n",
      " epoch 100/100 : loss = 15.187\n",
      " epoch 10/100 : loss = 12.302\n",
      " epoch 20/100 : loss = 11.204\n",
      " epoch 30/100 : loss = 10.484\n",
      " epoch 40/100 : loss = 10.022\n",
      " epoch 50/100 : loss = 9.715\n",
      " epoch 60/100 : loss = 9.493\n",
      " epoch 70/100 : loss = 9.321\n",
      " epoch 80/100 : loss = 9.181\n",
      " epoch 90/100 : loss = 9.064\n",
      " epoch 100/100 : loss = 8.964\n",
      " epoch 10/100 : loss = 19.041\n",
      " epoch 20/100 : loss = 18.992\n",
      " epoch 30/100 : loss = 18.947\n",
      " epoch 40/100 : loss = 18.906\n",
      " epoch 50/100 : loss = 18.866\n",
      " epoch 60/100 : loss = 18.826\n",
      " epoch 70/100 : loss = 18.784\n",
      " epoch 80/100 : loss = 18.744\n",
      " epoch 90/100 : loss = 18.693\n",
      " epoch 100/100 : loss = 18.641\n",
      " epoch 10/100 : loss = 16.541\n",
      " epoch 20/100 : loss = 15.534\n",
      " epoch 30/100 : loss = 15.335\n",
      " epoch 40/100 : loss = 15.244\n",
      " epoch 50/100 : loss = 15.188\n",
      " epoch 60/100 : loss = 15.148\n",
      " epoch 70/100 : loss = 15.118\n",
      " epoch 80/100 : loss = 15.094\n",
      " epoch 90/100 : loss = 15.075\n",
      " epoch 100/100 : loss = 15.059\n",
      " epoch 10/100 : loss = 17.734\n",
      " epoch 20/100 : loss = 16.560\n",
      " epoch 30/100 : loss = 16.039\n",
      " epoch 40/100 : loss = 15.777\n",
      " epoch 50/100 : loss = 15.624\n",
      " epoch 60/100 : loss = 15.523\n",
      " epoch 70/100 : loss = 15.452\n",
      " epoch 80/100 : loss = 15.399\n",
      " epoch 90/100 : loss = 15.358\n",
      " epoch 100/100 : loss = 15.325\n",
      " epoch 10/100 : loss = 13.938\n",
      " epoch 20/100 : loss = 13.162\n",
      " epoch 30/100 : loss = 12.862\n",
      " epoch 40/100 : loss = 12.682\n",
      " epoch 50/100 : loss = 12.557\n",
      " epoch 60/100 : loss = 12.462\n",
      " epoch 70/100 : loss = 12.386\n",
      " epoch 80/100 : loss = 12.325\n",
      " epoch 90/100 : loss = 12.275\n",
      " epoch 100/100 : loss = 12.234\n",
      " epoch 10/100 : loss = 13.664\n",
      " epoch 20/100 : loss = 12.990\n",
      " epoch 30/100 : loss = 12.729\n",
      " epoch 40/100 : loss = 12.574\n",
      " epoch 50/100 : loss = 12.468\n",
      " epoch 60/100 : loss = 12.389\n",
      " epoch 70/100 : loss = 12.328\n",
      " epoch 80/100 : loss = 12.278\n",
      " epoch 90/100 : loss = 12.235\n",
      " epoch 100/100 : loss = 12.197\n",
      " epoch 10/100 : loss = 12.702\n",
      " epoch 20/100 : loss = 12.308\n",
      " epoch 30/100 : loss = 12.231\n",
      " epoch 40/100 : loss = 12.196\n",
      " epoch 50/100 : loss = 12.171\n",
      " epoch 60/100 : loss = 12.151\n",
      " epoch 70/100 : loss = 12.131\n",
      " epoch 80/100 : loss = 12.113\n",
      " epoch 90/100 : loss = 12.095\n",
      " epoch 100/100 : loss = 12.076\n",
      " epoch 10/100 : loss = 12.943\n",
      " epoch 20/100 : loss = 12.542\n",
      " epoch 30/100 : loss = 12.272\n",
      " epoch 40/100 : loss = 12.189\n",
      " epoch 50/100 : loss = 12.155\n",
      " epoch 60/100 : loss = 12.132\n",
      " epoch 70/100 : loss = 12.114\n",
      " epoch 80/100 : loss = 12.099\n",
      " epoch 90/100 : loss = 12.084\n",
      " epoch 100/100 : loss = 12.069\n",
      " epoch 10/100 : loss = 13.503\n",
      " epoch 20/100 : loss = 13.363\n",
      " epoch 30/100 : loss = 13.292\n",
      " epoch 40/100 : loss = 13.248\n",
      " epoch 50/100 : loss = 13.215\n",
      " epoch 60/100 : loss = 13.188\n",
      " epoch 70/100 : loss = 13.164\n",
      " epoch 80/100 : loss = 13.142\n",
      " epoch 90/100 : loss = 13.122\n",
      " epoch 100/100 : loss = 13.102\n",
      " epoch 10/100 : loss = 13.620\n",
      " epoch 20/100 : loss = 13.384\n",
      " epoch 30/100 : loss = 13.265\n",
      " epoch 40/100 : loss = 13.178\n",
      " epoch 50/100 : loss = 13.115\n",
      " epoch 60/100 : loss = 13.070\n",
      " epoch 70/100 : loss = 13.035\n",
      " epoch 80/100 : loss = 13.006\n",
      " epoch 90/100 : loss = 12.982\n",
      " epoch 100/100 : loss = 12.960\n",
      " epoch 10/100 : loss = 18.723\n",
      " epoch 20/100 : loss = 18.559\n",
      " epoch 30/100 : loss = 18.459\n",
      " epoch 40/100 : loss = 18.392\n",
      " epoch 50/100 : loss = 18.342\n",
      " epoch 60/100 : loss = 18.303\n",
      " epoch 70/100 : loss = 18.270\n",
      " epoch 80/100 : loss = 18.241\n",
      " epoch 90/100 : loss = 18.215\n",
      " epoch 100/100 : loss = 18.190\n",
      " epoch 10/100 : loss = 18.636\n",
      " epoch 20/100 : loss = 18.476\n",
      " epoch 30/100 : loss = 18.380\n",
      " epoch 40/100 : loss = 18.301\n",
      " epoch 50/100 : loss = 18.240\n",
      " epoch 60/100 : loss = 18.216\n",
      " epoch 70/100 : loss = 18.199\n",
      " epoch 80/100 : loss = 18.183\n",
      " epoch 90/100 : loss = 18.167\n",
      " epoch 100/100 : loss = 18.151\n",
      " epoch 10/100 : loss = 15.385\n",
      " epoch 20/100 : loss = 15.249\n",
      " epoch 30/100 : loss = 15.184\n",
      " epoch 40/100 : loss = 15.142\n",
      " epoch 50/100 : loss = 15.109\n",
      " epoch 60/100 : loss = 15.080\n",
      " epoch 70/100 : loss = 15.055\n",
      " epoch 80/100 : loss = 15.033\n",
      " epoch 90/100 : loss = 15.013\n",
      " epoch 100/100 : loss = 14.996\n",
      " epoch 10/100 : loss = 12.576\n",
      " epoch 20/100 : loss = 11.982\n",
      " epoch 30/100 : loss = 11.651\n",
      " epoch 40/100 : loss = 11.433\n",
      " epoch 50/100 : loss = 11.346\n",
      " epoch 60/100 : loss = 11.314\n",
      " epoch 70/100 : loss = 11.293\n",
      " epoch 80/100 : loss = 11.276\n",
      " epoch 90/100 : loss = 11.263\n",
      " epoch 100/100 : loss = 11.251\n",
      " epoch 10/100 : loss = 14.940\n",
      " epoch 20/100 : loss = 14.681\n",
      " epoch 30/100 : loss = 14.520\n",
      " epoch 40/100 : loss = 14.397\n",
      " epoch 50/100 : loss = 14.299\n",
      " epoch 60/100 : loss = 14.250\n",
      " epoch 70/100 : loss = 14.233\n",
      " epoch 80/100 : loss = 14.220\n",
      " epoch 90/100 : loss = 14.208\n",
      " epoch 100/100 : loss = 14.196\n",
      " epoch 10/100 : loss = 15.166\n",
      " epoch 20/100 : loss = 15.022\n",
      " epoch 30/100 : loss = 14.952\n",
      " epoch 40/100 : loss = 14.907\n",
      " epoch 50/100 : loss = 14.876\n",
      " epoch 60/100 : loss = 14.852\n",
      " epoch 70/100 : loss = 14.832\n",
      " epoch 80/100 : loss = 14.815\n",
      " epoch 90/100 : loss = 14.800\n",
      " epoch 100/100 : loss = 14.786\n",
      " epoch 10/100 : loss = 14.631\n",
      " epoch 20/100 : loss = 14.353\n",
      " epoch 30/100 : loss = 14.245\n",
      " epoch 40/100 : loss = 14.194\n",
      " epoch 50/100 : loss = 14.165\n",
      " epoch 60/100 : loss = 14.144\n",
      " epoch 70/100 : loss = 14.127\n",
      " epoch 80/100 : loss = 14.112\n",
      " epoch 90/100 : loss = 14.099\n",
      " epoch 100/100 : loss = 14.086\n",
      " epoch 10/100 : loss = 9.306\n",
      " epoch 20/100 : loss = 9.135\n",
      " epoch 30/100 : loss = 9.048\n",
      " epoch 40/100 : loss = 8.991\n",
      " epoch 50/100 : loss = 8.951\n",
      " epoch 60/100 : loss = 8.923\n",
      " epoch 70/100 : loss = 8.901\n",
      " epoch 80/100 : loss = 8.883\n",
      " epoch 90/100 : loss = 8.869\n",
      " epoch 100/100 : loss = 8.856\n",
      " epoch 10/100 : loss = 18.543\n",
      " epoch 20/100 : loss = 18.245\n",
      " epoch 30/100 : loss = 18.152\n",
      " epoch 40/100 : loss = 18.108\n",
      " epoch 50/100 : loss = 18.081\n",
      " epoch 60/100 : loss = 18.062\n",
      " epoch 70/100 : loss = 18.046\n",
      " epoch 80/100 : loss = 18.032\n",
      " epoch 90/100 : loss = 18.020\n",
      " epoch 100/100 : loss = 18.008\n",
      " epoch 10/100 : loss = 15.394\n",
      " epoch 20/100 : loss = 14.393\n",
      " epoch 30/100 : loss = 13.754\n",
      " epoch 40/100 : loss = 13.301\n",
      " epoch 50/100 : loss = 12.964\n",
      " epoch 60/100 : loss = 12.705\n",
      " epoch 70/100 : loss = 12.501\n",
      " epoch 80/100 : loss = 12.337\n",
      " epoch 90/100 : loss = 12.201\n",
      " epoch 100/100 : loss = 12.087\n",
      " epoch 10/100 : loss = 11.521\n",
      " epoch 20/100 : loss = 10.859\n",
      " epoch 30/100 : loss = 10.631\n",
      " epoch 40/100 : loss = 10.495\n",
      " epoch 50/100 : loss = 10.396\n",
      " epoch 60/100 : loss = 10.320\n",
      " epoch 70/100 : loss = 10.261\n",
      " epoch 80/100 : loss = 10.215\n",
      " epoch 90/100 : loss = 10.178\n",
      " epoch 100/100 : loss = 10.148\n",
      " epoch 10/100 : loss = 12.240\n",
      " epoch 20/100 : loss = 11.299\n",
      " epoch 30/100 : loss = 10.978\n",
      " epoch 40/100 : loss = 10.791\n",
      " epoch 50/100 : loss = 10.659\n",
      " epoch 60/100 : loss = 10.560\n",
      " epoch 70/100 : loss = 10.484\n",
      " epoch 80/100 : loss = 10.423\n",
      " epoch 90/100 : loss = 10.373\n",
      " epoch 100/100 : loss = 10.331\n",
      " epoch 10/100 : loss = 12.860\n",
      " epoch 20/100 : loss = 12.201\n",
      " epoch 30/100 : loss = 11.883\n",
      " epoch 40/100 : loss = 11.674\n",
      " epoch 50/100 : loss = 11.539\n",
      " epoch 60/100 : loss = 11.450\n",
      " epoch 70/100 : loss = 11.388\n",
      " epoch 80/100 : loss = 11.343\n",
      " epoch 90/100 : loss = 11.308\n",
      " epoch 100/100 : loss = 11.281\n",
      " epoch 10/100 : loss = 11.488\n",
      " epoch 20/100 : loss = 11.066\n",
      " epoch 30/100 : loss = 10.840\n",
      " epoch 40/100 : loss = 10.690\n",
      " epoch 50/100 : loss = 10.582\n",
      " epoch 60/100 : loss = 10.500\n",
      " epoch 70/100 : loss = 10.436\n",
      " epoch 80/100 : loss = 10.383\n",
      " epoch 90/100 : loss = 10.340\n",
      " epoch 100/100 : loss = 10.303\n",
      " epoch 10/100 : loss = 10.924\n",
      " epoch 20/100 : loss = 10.525\n",
      " epoch 30/100 : loss = 10.255\n",
      " epoch 40/100 : loss = 10.063\n",
      " epoch 50/100 : loss = 9.921\n",
      " epoch 60/100 : loss = 9.812\n",
      " epoch 70/100 : loss = 9.725\n",
      " epoch 80/100 : loss = 9.654\n",
      " epoch 90/100 : loss = 9.595\n",
      " epoch 100/100 : loss = 9.544\n",
      " epoch 10/100 : loss = 9.354\n",
      " epoch 20/100 : loss = 9.337\n",
      " epoch 30/100 : loss = 9.321\n",
      " epoch 40/100 : loss = 9.304\n",
      " epoch 50/100 : loss = 9.288\n",
      " epoch 60/100 : loss = 9.271\n",
      " epoch 70/100 : loss = 9.255\n",
      " epoch 80/100 : loss = 9.239\n",
      " epoch 90/100 : loss = 9.223\n",
      " epoch 100/100 : loss = 9.208\n"
     ]
    }
   ],
   "source": [
    "results = {}\n",
    "for machine in file_name_list:\n",
    "        # print(machine)\n",
    "        train_data,test_data, labels = datas[machine]\n",
    "        ratio = np.count_nonzero(labels == 100)/labels.shape[0]+0.000001\n",
    "        dagmm_model = dagmm.DAGMM(epoch_size=100, contamination=ratio, comp_hiddens=[2,1])\n",
    "        dagmm_model.fit(train_data)\n",
    "        anomaly_score = dagmm_model.decision_function(test_data).T\n",
    "        result = bf_search(labels, anomaly_score, verbose = False, is_adjust=True)\n",
    "        results[machine] = result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "precision mean: 0.7499\n",
      "recall mean: 0.8389\n",
      "f1 mean: 0.7833\n",
      "f1* mean: 0.7919\n"
     ]
    }
   ],
   "source": [
    "f1_scores = [item['f1-score'] for item in list(results.values())]\n",
    "precisions = [item['precision'] for item in list(results.values())]\n",
    "recalls = [item['recall'] for item in list(results.values())]\n",
    "TNs = [item['TN'] for item in list(results.values())]\n",
    "TPs = [item['TP'] for item in list(results.values())]\n",
    "FNs = [item['FN'] for item in list(results.values())]\n",
    "FPs = [item['FP'] for item in list(results.values())]\n",
    "f1_mean = round(np.mean(f1_scores).item(),4)\n",
    "precision_mean = round(np.mean(precisions),4)\n",
    "recall_mean = round(np.mean(recalls),4)\n",
    "f1_star = round(2 * precision_mean * recall_mean / (precision_mean + recall_mean + 0.00001),4)\n",
    "print(f\"precision mean: {precision_mean}\")\n",
    "print(f\"recall mean: {recall_mean}\")\n",
    "print(f\"f1 mean: {f1_mean}\")\n",
    "print(f\"f1* mean: {f1_star}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "yjq-3.6",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
